package com.lanou3g.dllo.baidumusic.musiclibrary.mv;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseFragment;
import com.lanou3g.dllo.baidumusic.tools.MyRvOnClickListener;
import com.lanou3g.dllo.baidumusic.tools.MyVolley;
import com.lanou3g.dllo.baidumusic.urlvalues.UrlValues;


/**
 * Created by dllo on 16/6/17.
 */
public class MVFragment extends BaseFragment {

    private MvAdapter mvAdapter;
    private RecyclerView mvRecyclerview;
    private SwipeRefreshLayout swipeRefreshLayout;
    private MVBean mvBean;
    int number = 1;
    private GridLayoutManager gridLayoutManager;
    private int lastVisibleItem;
    private int num = 2;
    private TextView mvNewestShow,mvHottestShow;

    @Override
    public int setLayout() {
        return R.layout.fragment_mv;
    }

    @Override
    public void initView(View view) {

        mvRecyclerview = (RecyclerView) view.findViewById(R.id.mv_recyclerview);
        mvAdapter = new MvAdapter(context);
        mvBean = new MVBean();
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.mv_layout);
        mvNewestShow = (TextView) view.findViewById(R.id.mv_newest_show_tv);
        mvHottestShow = (TextView) view.findViewById(R.id.mv_hottest_show_tv);

    }

    @Override
    public void initData() {

        refresh();
        mvNewestShow.setTextColor(Color.GREEN);
        mvHottestShow.setTextColor(Color.GRAY);
        String url = UrlValues.MV_URL_FRONT + number + UrlValues.MV_URL_BEHIND;
        MyVolley.getMyVolley().initGson(url, new MyVolley.GetGsonListener<MVBean>() {
            @Override
            public void getGsonListener(MVBean data) {
                mvBean = data;
                mvAdapter.setMvBean(data);
                mvRecyclerview.setAdapter(mvAdapter);
                gridLayoutManager = new GridLayoutManager(context, 2);
                mvRecyclerview.setLayoutManager(gridLayoutManager);
            }
        }, MVBean.class);

        // 跳转到播放Mv的Activity,并播放mv
        mvAdapter.setMyRvOnClickListener(new MyRvOnClickListener() {
            @Override
            public void onClick(int position) {
                String url = UrlValues.MV_PLAY_FRONT + mvAdapter.getMvBean().getResult().getMv_list().get(position).getMv_id() + UrlValues.MV_PLAY_BEHIND;
                MyVolley.getMyVolley().initGson(url, new MyVolley.GetGsonListener<MVDeatilBean>() {
                    @Override
                    public void getGsonListener(MVDeatilBean data) {
                        String playUrl = data.getResult().getVideo_info().getSourcepath().substring(31);
                        playUrl = "http://www.yinyuetai.com/mv/video-url/" + playUrl;
                        Intent intentVideo = new Intent(getActivity(),MvActivity.class);
                        intentVideo.putExtra("video",playUrl);
                        startActivity(intentVideo);
                    }
                },MVDeatilBean.class);
            }
        });

        // 最新监听
        mvNewestShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mvNewestShow.setTextColor(Color.GREEN);
                mvHottestShow.setTextColor(Color.GRAY);
                refresh();
                String url = UrlValues.MV_URL_FRONT + number + UrlValues.MV_URL_BEHIND;
                MyVolley.getMyVolley().initGson(url, new MyVolley.GetGsonListener<MVBean>() {
                    @Override
                    public void getGsonListener(MVBean data) {
                        mvBean = data;
                        mvAdapter.setMvBean(data);
                        mvRecyclerview.setAdapter(mvAdapter);
                        gridLayoutManager = new GridLayoutManager(context, 2);
                        mvRecyclerview.setLayoutManager(gridLayoutManager);
                    }
                }, MVBean.class);

                // 跳转到播放Mv的Activity,并播放mv
                mvAdapter.setMyRvOnClickListener(new MyRvOnClickListener() {
                    @Override
                    public void onClick(int position) {
                        String url = UrlValues.MV_PLAY_FRONT + mvAdapter.getMvBean().getResult().getMv_list().get(position).getMv_id() + UrlValues.MV_PLAY_BEHIND;
                        MyVolley.getMyVolley().initGson(url, new MyVolley.GetGsonListener<MVDeatilBean>() {
                            @Override
                            public void getGsonListener(MVDeatilBean data) {
                                String playUrl = data.getResult().getVideo_info().getSourcepath().substring(31);
                                playUrl = "http://www.yinyuetai.com/mv/video-url/" + playUrl;
                                Intent intentVideo = new Intent(getActivity(),MvActivity.class);
                                intentVideo.putExtra("video",playUrl);
                                startActivity(intentVideo);
                            }

                        },MVDeatilBean.class);
                    }
                });
            }
        });

        // 最热监听
        mvHottestShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mvNewestShow.setTextColor(Color.GRAY);
                mvHottestShow.setTextColor(Color.GREEN);
                refresh();
                String url = UrlValues.MV_HOT_FRONT + number + UrlValues.MV_HOT_BEHIND;
                MyVolley.getMyVolley().initGson(url, new MyVolley.GetGsonListener<MVBean>() {
                    @Override
                    public void getGsonListener(MVBean data) {
                        mvBean = data;
                        mvAdapter.setMvBean(data);
                        mvRecyclerview.setAdapter(mvAdapter);
                        gridLayoutManager = new GridLayoutManager(context, 2);
                        mvRecyclerview.setLayoutManager(gridLayoutManager);
                    }
                }, MVBean.class);

                // 跳转到播放Mv的Activity,并播放mv
                mvAdapter.setMyRvOnClickListener(new MyRvOnClickListener() {
                    @Override
                    public void onClick(int position) {
                        String url = UrlValues.MV_PLAY_FRONT + mvAdapter.getMvBean().getResult().getMv_list().get(position).getMv_id() + UrlValues.MV_PLAY_BEHIND;
                        MyVolley.getMyVolley().initGson(url, new MyVolley.GetGsonListener<MVDeatilBean>() {
                            @Override
                            public void getGsonListener(MVDeatilBean data) {
                                String playUrl = data.getResult().getVideo_info().getSourcepath().substring(31);
                                playUrl = "http://www.yinyuetai.com/mv/video-url/" + playUrl;
                                Intent intentVideo = new Intent(getActivity(),MvActivity.class);
                                intentVideo.putExtra("video",playUrl);
                                startActivity(intentVideo);
                            }

                        },MVDeatilBean.class);
                    }
                });
            }
        });
    }

    // 上拉加载方法
    private void refresh() {
        swipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources().getDisplayMetrics()));
        mvRecyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mvAdapter.getItemCount()) {
                    String url = UrlValues.MV_URL_FRONT + num + UrlValues.MV_URL_BEHIND;
                    MyVolley.getMyVolley().initGson(url, new MyVolley.GetGsonListener<MVBean>() {
                        @Override
                        public void getGsonListener(MVBean data) {
                            swipeRefreshLayout.setRefreshing(false);
                            mvBean = data;
                            mvAdapter.addMvBean(mvBean);
                        }
                    }, MVBean.class);
                    swipeRefreshLayout.setRefreshing(true);
                    num += 1;
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = gridLayoutManager.findLastVisibleItemPosition();
            }
        });
    }


}
